package org.swqsbssddqs.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.swqsbssddqs.shortlink.project.common.convention.result.Result;
import org.swqsbssddqs.shortlink.project.common.convention.result.Results;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkCreatReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkPageReqDTO;
import org.swqsbssddqs.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import org.swqsbssddqs.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import org.swqsbssddqs.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import org.swqsbssddqs.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import org.swqsbssddqs.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import org.swqsbssddqs.shortlink.project.handler.CustomBlockHandler;
import org.swqsbssddqs.shortlink.project.service.ShortLinkService;

import java.util.List;

@RestController
@AllArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortLinkService;
    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreatReqDTO shortLinkCreatReqDTOS){
        return Results.success(shortLinkService.createShortLink(shortLinkCreatReqDTOS));
    }
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO shortLinkBatchCreatReqDTOS){
        return Results.success(shortLinkService.batchCreateShortLink(shortLinkBatchCreatReqDTOS));
    }
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO){
        return Results.success(shortLinkService.pageShortLink(shortLinkPageReqDTO));
    }
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam){
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParam));
    }

    @PostMapping("/api/short-link/v1/update")//换成putMapping试一下
    public Result<Void> listGroupShortLinkCount(@RequestBody ShortLinkUpdateReqDTO shortLinkUpdateReqDTO){
        return Results.success(shortLinkService.updateShortLink(shortLinkUpdateReqDTO));
    }
    @GetMapping("/{short-uri}")
    public void redirectUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) {
        shortLinkService.redirectUrl(shortUri, request, response);
    }

}
